/**
 * 飞机基类
 */
var BasePlane = require('./../BasePlane');
var EnumBody = require('./../../Enum/EnumBody');
var EnumPlane = require('./../../Enum/EnumPlane');
module.exports = cc.Class(
{
    extends: BasePlane,

    onLoad()
    {
        this._super();
        this.setEnumPlane(EnumPlane.SCORE);
        this.node.x = -cc.winSize.width * 1.1;
        this.node.y = -cc.winSize.height * 1.1;
    },

    start()
    {
        var spacex = 0;
        var tmpTag = this.getTag() % 3;
        //0-1-2
        if (1 == tmpTag)
        {
            spacex = -1;
        }
        else if (2 == tmpTag)
        {
            spacex = 1;
        }

        this.node.x = -cc.winSize.width * 0.5 * spacex;
        this.node.y = cc.winSize.height * 0.5;
        var bezier = [
            cc.v2(-cc.winSize.width * 0.5 * spacex, cc.winSize.height * 0.5),
            cc.v2(0, 0),
            cc.v2(-cc.winSize.width * 0.5 * spacex, -cc.winSize.height * 0.5),
        ];
        var bezierForward = cc.bezierTo(10, bezier);

        var actArray = [bezierForward,
            cc.callFunc(function()
            {
                this.node.destroy();
            }.bind(this))
        ];
        var seq = cc.sequence(actArray);
        this.node.runAction(seq);
    },

    update(dt)
    {
        if (null == this.lastP)
        {
            this.lastP = cc.v2(this.node.x, this.node.y);
            return;
        }
        var currentP = cc.v2(this.node.x, this.node.y);

        var deltaP = currentP.sub(this.lastP)
        var angle = deltaP.signAngle(cc.v2(0, 1));
        this.node.rotation = angle * (180 / Math.PI) + 180;

    },


    onCollisionEnter: function(other)
    {
        var body = this._super(other);
        if (null == body)
        {
            return;
        }

        var enumBody = body.getEnumBody();
        if (EnumBody.PLANE == enumBody)
        {
            var plane = other.getComponent(BasePlane);
            if (null != plane && EnumPlane.MY == plane.getEnumPlane())
            {
                this.playBomb();
                this.node.destroy();
            }
        }

    },
});